package processingdata.airport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import processing.core.PVector;
import Control.Airport;

public class AirportSerializer {
	
	static ArrayList<Airport> airports=new ArrayList<Airport>();
	public static void main(String args[]) throws IOException
	{
		
		try
		{
			//BufferedReader inputReader=new BufferedReader(new FileReader(new File("data/airport.data")));
			BufferedReader inputReader=new BufferedReader(new FileReader(new File("data/airportfinal.data")));
			
			while(inputReader.ready())
			{
				String inputReaderLine=inputReader.readLine();
				System.out.println(inputReaderLine);
				String inputReaderLineParts[]=inputReaderLine.split("\t");
				String fourCode=inputReaderLineParts[0];
				String threeCode=inputReaderLineParts[1];
				String airportName=inputReaderLineParts[2];
				String place=inputReaderLineParts[3];
				String country=inputReaderLineParts[4];
				float latitude=Float.parseFloat(inputReaderLineParts[5]);
				float longitude=Float.parseFloat(inputReaderLineParts[6]);
				
				if(!(latitude==0 && longitude==0))
				{
					PVector coordinates=new PVector(latitude, longitude);
					
					Airport airport=new Airport();
					airport.setCoordinates(coordinates);
					airport.setName(airportName);
					airport.setPlace(place);
					airport.setAirportId(threeCode);
					airports.add(airport);	
				}
							
			}
			
			serializeAirports();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	static void serializeAirports() throws FileNotFoundException, IOException
	{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("data/airports.ser")));
		oos.writeObject(airports);
		oos.close();
	}

}
